package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.x;

import a66weding.com.jiehuntong.MainActivity;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.activity.me.MeAttentionActivity;
import a66weding.com.jiehuntong.activity.me.MeCollectActivity;
import a66weding.com.jiehuntong.activity.me.MeGeneralizeActivity;
import a66weding.com.jiehuntong.activity.me.MeIndentActivity;
import a66weding.com.jiehuntong.activity.me.MeSettingActivity;
import a66weding.com.jiehuntong.activity.me.MeShoppingCartActivity;
import a66weding.com.jiehuntong.activity.me.MeShowUserActivity;
import a66weding.com.jiehuntong.activity.me.MeUserinfoActivity;
import a66weding.com.jiehuntong.activity.toolsbudget.ToolsBudgetActivity;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;
import a66weding.com.jiehuntong.widgets.CircularImage;

/**
 * 我的
 * Created by y on 2016/5/17.
 */
public class MinePager extends BasePager implements View.OnClickListener {
    private RelativeLayout rl_mine_top;//顶部结婚人
    private RelativeLayout rl_mine_indent;//订单
    private RelativeLayout rl_mine_shoppingcart;//购物车
    private RelativeLayout rl_mine_wedbudget;//结婚预算
    private RelativeLayout rl_mine_twocode;//二维码
    private RelativeLayout rl_mine_collect;//收藏
    private RelativeLayout rl_mine_attention;//关注
    private RelativeLayout rl_mine_setting;//设置
    private RelativeLayout mUserInfoIv;//用户信息
    private RelativeLayout mNologinRlay;//没有登录显示
    private RelativeLayout mloginRlay;//登录显示
    private TextView mToLoginTv;//登录显示
    private ImageView iv_homeme_invview;
    private TextView mNickName;//昵称显示
    private TextView mMarriedDay;
    private ImageView mHeadlineIv;
    private boolean isLogin;
    private CircularImage mUserPhoto;
    private int PAGER_BASE_REQUEST_LOGIN_CODE = 5000;
    //头像的名字
    private View mMeView;
    private User user;

    public MinePager(Activity activity) {
        super(activity);
    }

    @Override
    protected View getView() {
        mMeView = View.inflate(mActivity, R.layout.pager_me, null);
        rl_mine_top = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_top);
        rl_mine_indent = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_indent);
        rl_mine_shoppingcart = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_shoppingcart);
        rl_mine_wedbudget = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_wedbudget);
        rl_mine_twocode = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_twocode);
        rl_mine_collect = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_collect);
        rl_mine_attention = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_attention);
        rl_mine_setting = (RelativeLayout) mMeView.findViewById(R.id.rl_mine_setting);
        mUserInfoIv = (RelativeLayout) mMeView.findViewById(R.id.rlay_me_touserinfo);
        mNologinRlay = (RelativeLayout) mMeView.findViewById(R.id.rlay_me_nologinhead);
        mloginRlay = (RelativeLayout) mMeView.findViewById(R.id.rlay_me_loginhead);
        iv_homeme_invview = (ImageView) mMeView.findViewById(R.id.iv_homeme_invview);
        mNickName = (TextView) mMeView.findViewById(R.id.tv_homemeinv_name);
        mMarriedDay = (TextView) mMeView.findViewById(R.id.tv_married_day);
        mUserPhoto = (CircularImage) mMeView.findViewById(R.id.iv_homeme_invview);
        mHeadlineIv = (ImageView) mMeView.findViewById(R.id.iv_homeme_msg);
        rl_mine_top.setOnClickListener(this);
        rl_mine_indent.setOnClickListener(this);
        rl_mine_shoppingcart.setOnClickListener(this);
        rl_mine_wedbudget.setOnClickListener(this);
        rl_mine_twocode.setOnClickListener(this);
        rl_mine_collect.setOnClickListener(this);
        rl_mine_attention.setOnClickListener(this);
        rl_mine_setting.setOnClickListener(this);
        mNologinRlay.setOnClickListener(this);
        iv_homeme_invview.setOnClickListener(this);
        mHeadlineIv.setVisibility(View.GONE);
        return mMeView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_mine_indent://我的-订单界面
                if (isLogin) {
                    Intent intent = new Intent(mActivity, MeIndentActivity.class);
                    mActivity.startActivity(intent);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 2);
                }
                break;
            case R.id.rl_mine_shoppingcart:
                if (isLogin) {
                    MainActivity mainActivity = (MainActivity) ((GlobalApplication) mActivity.getApplication()).getMainActivityManager().getActivity("MainActivity");
                    if (mainActivity != null) {
                        mainActivity.isQuGuangguang = false;
                    }
                    Intent intent = new Intent(mActivity, MeShoppingCartActivity.class);
//                    intent.putExtra("isCanBack",true);
                    mActivity.startActivityForResult(intent, MainActivity.getResultForCartCode);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 3);
                }
                break;
            case R.id.rl_mine_wedbudget://结婚预算
                if (isLogin) {
                    Intent intent = new Intent(mActivity, ToolsBudgetActivity.class);
                    mActivity.startActivity(intent);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 4);
                }
                break;
            case R.id.rl_mine_twocode://二维码推广
                if (isLogin) {
                    Intent intent = new Intent(mActivity, MeGeneralizeActivity.class);
                    mActivity.startActivity(intent);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 5);
                }
                break;
            case R.id.rl_mine_collect://收藏
                if (isLogin) {
                    Intent intent = new Intent(mActivity, MeCollectActivity.class);
                    mActivity.startActivity(intent);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 6);
                }
                break;
            case R.id.rl_mine_attention://关注
                if (isLogin) {
                    Intent intent = new Intent(mActivity, MeAttentionActivity.class);
                    mActivity.startActivity(intent);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 7);
                }
                break;
            case R.id.rl_mine_setting:
                Intent intents = new Intent(mActivity, MeSettingActivity.class);
                mActivity.startActivity(intents);
                break;
            case R.id.tv_mine_budgetview:
                break;
            case R.id.rl_mine_top://用户信息
                if (isLogin) {
                    Intent intent = new Intent(mActivity, MeShowUserActivity.class);
                    mActivity.startActivityForResult(intent, MainActivity.getResultForMeCode);
                } else {
                    showLoginDialog(mActivity, PAGER_BASE_REQUEST_LOGIN_CODE + 8);
                }
                break;
            case R.id.tv_me_tologin://去登陆
                Intent intent = new Intent(mActivity, LoginActivity.class);
                mActivity.startActivityForResult(intent, PAGER_BASE_REQUEST_LOGIN_CODE + 1);
                break;
            case R.id.iv_homeme_invview://点击照片可以个人信息界面
                Intent intent1 = new Intent(mActivity, MeUserinfoActivity.class);
                mActivity.startActivityForResult(intent1, MainActivity.getResultForMeCode);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== LoginActivity.LOGIN_OK){
            initData();
        }
         if (requestCode == (PAGER_BASE_REQUEST_LOGIN_CODE + 2) && resultCode == LoginActivity.LOGIN_OK) {
             Intent intent = new Intent(mActivity, MeIndentActivity.class);
             mActivity.startActivity(intent);
        } else if (requestCode == (PAGER_BASE_REQUEST_LOGIN_CODE + 3) && resultCode == LoginActivity.LOGIN_OK) {
             MainActivity mainActivity = (MainActivity) ((GlobalApplication) mActivity.getApplication()).getMainActivityManager().getActivity("MainActivity");
             if (mainActivity != null) {
                 mainActivity.isQuGuangguang = false;
             }
             Intent intent = new Intent(mActivity, MeShoppingCartActivity.class);
             mActivity.startActivityForResult(intent, MainActivity.getResultForCartCode);
        } else if (requestCode == (PAGER_BASE_REQUEST_LOGIN_CODE + 4) && resultCode == LoginActivity.LOGIN_OK) {
             Intent intent = new Intent(mActivity, ToolsBudgetActivity.class);
             mActivity.startActivity(intent);
        } else if (requestCode == (PAGER_BASE_REQUEST_LOGIN_CODE + 5) && resultCode == LoginActivity.LOGIN_OK) {
             Intent intent = new Intent(mActivity, MeGeneralizeActivity.class);
             mActivity.startActivity(intent);
        } else if (requestCode == (PAGER_BASE_REQUEST_LOGIN_CODE + 6) && resultCode == LoginActivity.LOGIN_OK) {
             Intent intent = new Intent(mActivity, MeCollectActivity.class);
             mActivity.startActivity(intent);
        } else if (requestCode == (PAGER_BASE_REQUEST_LOGIN_CODE + 7) && resultCode == LoginActivity.LOGIN_OK) {
             Intent intent = new Intent(mActivity, MeAttentionActivity.class);
             mActivity.startActivity(intent);
        } else if (requestCode == (PAGER_BASE_REQUEST_LOGIN_CODE + 8) && resultCode == LoginActivity.LOGIN_OK) {
             Intent intent = new Intent(mActivity, MeShowUserActivity.class);
             mActivity.startActivityForResult(intent, MainActivity.getResultForMeCode);
        }
    }

    @Override
    public void initData() {
        isLogin = ((GlobalApplication) mActivity.getApplication()).isLogin();
        if (isLogin) {
            mNologinRlay.setVisibility(View.GONE);
            mloginRlay.setVisibility(View.VISIBLE);
            user = DataWareHourse.getInstance().getmUser();
            if (user != null) {
                User.DataBean dataBean = user.getData().get(0);
                if (dataBean != null) {
                    if ((!TextUtils.isEmpty(dataBean.getNvc_Name())) && (!TextUtils.isEmpty(dataBean.getNvc_WeddingDate()))) {
                        mNickName.setText(dataBean.getNvc_Name());
                        mMarriedDay.setText("婚期: " + dataBean.getNvc_WeddingDate());
                    } else {
                        mNickName.setText("昵称");
                        mMarriedDay.setText("婚期: 请设置您的婚期");
                    }
                    if (dataBean.getNvc_TitlePhoto() != null) {
                        x.image().bind(mUserPhoto, dataBean.getNvc_TitlePhoto());
                    }
                }
            }
        } else {
            mloginRlay.setVisibility(View.GONE);
            mNologinRlay.setVisibility(View.VISIBLE);
            mToLoginTv = (TextView) mMeView.findViewById(R.id.tv_me_tologin);
            mToLoginTv.setOnClickListener(this);
            mNickName.setText("昵称");
            mMarriedDay.setText("婚期: 请设置您的婚期");
        }
    }

    public void showLoginDialog(Context context, final int requestLoginCode) {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(context, "亲，需要登录哦~", "去登录", "取消", R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent(mActivity, LoginActivity.class);
                mActivity.startActivityForResult(intent, requestLoginCode);
                confirmDialog.dismiss();
            }

            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }
}
